package com.qianfeng.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qianfeng.user.bean.Address;
import com.qianfeng.user.bean.User;
import com.qianfeng.user.bean.UserInfo;
import com.qianfeng.user.service.AddressService;
import com.qianfeng.user.service.UserInfoService;
import com.qianfeng.user.service.UserService;
import com.qianfeng.utils.Response;

@Controller
@RequestMapping("/user/")
public class UserController {
	private UserService userService = new UserService();
	private UserInfoService userInfoService = new UserInfoService();
	private AddressService addressService = new AddressService();

	@RequestMapping("toLogin")
	public String toLogin() {
		return "login";
	}

	@RequestMapping("toRegister")
	public String toRegister() {
		return "register";
	}

	@ResponseBody
	@RequestMapping("checkUserName")
	public Response checkUserName(User user) {
		boolean flg = userService.checkUserName(user);

		Response rs = new Response();
		if (flg) {
			rs.setCode(1);
			rs.setMessage("恭喜您,用户名可以使用");

		} else {
			rs.setCode(-1);
			rs.setMessage("对不起,用户名已被注册");
		}
		return rs;
	}

	@ResponseBody
	@RequestMapping("register")
	public Response register(User user, HttpServletRequest req) {
		Response rs = new Response();

		if (user.getUserName() == null || "".equals(user.getUserName().trim())) { // 判断用户名不能为null和空
			rs.setCode(-1);
			rs.setMessage("对不起,用户名不能为空");
			return rs;
		}

		// 保存用户名密码前还要去验证一下用户名是否可用
		boolean flg = userService.checkUserName(user);
		if (!flg) {
			rs.setCode(-1);
			rs.setMessage("对不起,用户名已经被注册");

			return rs;
		}

		if (user.getPassword() == null || "".equals(user.getPassword().trim())) {
			rs.setCode(-1);
			rs.setMessage("对不起,密码不能为空");
			return rs;
		}

		String repassword = req.getParameter("repassword");
		if (!user.getPassword().equals(repassword)) {
			rs.setCode(-1);
			rs.setMessage("对不起,两次密码不一致");
			return rs;
		}

		flg = userService.saveUser(user);
		if (flg) {
			rs.setCode(1);
			rs.setMessage("恭喜您,注册成功");
		} else {
			rs.setCode(-1);
			rs.setMessage("对不起,注册失败");
		}

		return rs;

	}

	@ResponseBody
	@RequestMapping("login")
	public Response login(User user, HttpServletRequest req) {
		Response rs = new Response();

		/*
		 * String imgCode = req.getParameter("imgCode");
		 * if(imgCode==null||"".equals(imgCode.trim())) { //判断用户名不能为null和空
		 * msg.setCode(-1); msg.setMessage("对不起,验证码不能为空");
		 * resp.getOutputStream().write(msg.toString().getBytes()); return ; } String
		 * code = (String) req.getSession().getAttribute("imgCode");
		 * if(!imgCode.equals(code)) { msg.setCode(-1); msg.setMessage("对不起,验证码错误");
		 * resp.getOutputStream().write(msg.toString().getBytes()); return ; }
		 */

		if (user.getUserName() == null || "".equals(user.getUserName().trim())) { // 判断用户名不能为null和空
			rs.setCode(-1);
			rs.setMessage("对不起,用户名不能为空");
			return rs;
		}

		if (user.getPassword() == null || "".equals(user.getPassword().trim())) {
			rs.setCode(-1);
			rs.setMessage("对不起,密码不能为空");

			return rs;
		}

		User user1 = userService.getUserByUserNameAndPassword(user);

		if (user1 != null) {
			req.getSession().setAttribute("user", user1);
			rs.setCode(1);
			rs.setMessage("恭喜您,登录成功");
		} else {
			rs.setCode(-1);
			rs.setMessage("对不起, 用户名密码不正确");
		}
		return rs;
	}

	@RequestMapping("logOut")
	public String logOut(HttpSession session) {
		session.removeAttribute("user");
		return "redirect:/goods/toMainPage";
	}

	@RequestMapping("toUserInfoPage")
	public String toUserInfoPage(HttpSession session, HttpServletRequest req) {
		User user = (User) session.getAttribute("user");

		if (user == null) {
			return "login";
		} else {

			UserInfo userInfo = userInfoService.getUserInfoByUserId(user.getId());
			req.setAttribute("userInfo", userInfo);

			List<Address> addressList = addressService.getAddressesByUserId(user.getId());
			req.setAttribute("addressList", addressList);

			return "userInfo";
		}
	}

	@RequestMapping("updateUserInfo")
	public String updateUserInfo(HttpSession session, UserInfo userInfo) {
		User user = (User) session.getAttribute("user");
		if (user != null) {
			/*
			 * String inco = req.getParameter("inco"); String nickName =
			 * req.getParameter("nickName"); System.out.println(nickName); String cellphone
			 * = req.getParameter("cellphone"); String qq = req.getParameter("qq"); String
			 * email = req.getParameter("email");
			 * 
			 * UserInfo userInfo = new UserInfo(); userInfo.setInco(inco);
			 * userInfo.setNickName(nickName); userInfo.setCellphone(cellphone);
			 * userInfo.setQq(qq); userInfo.setEmail(email);
			 * userInfo.setUserId(user.getId());
			 */

			userInfo.setUserId(user.getId());
			userInfoService.updateUserInfo(userInfo);
			user.setInco(userInfo.getInco()); // 更新个人信息之后, 将session中user对象中的头像更新
			return "redirect:/goods/toMainPage";
		} else {
			return "login";
		}
	}
}
